package com.example.common.component;


import com.example.common.control.ICallback;
import com.example.common.model.GameApiGameResp;
import com.example.common.model.GameApiGetGameResp;
import com.example.common.model.GameApiRankResp;
import com.example.common.model.GameApiSearchResp;
import com.example.common.model.GameResp;
import com.example.common.model.MyResponse;
import com.example.common.network.GameReq;
import com.example.common.network.RetrofitServiceManager;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class GameCtrlCom {
    private GameReq gameReq;

    public GameCtrlCom(){
        gameReq= RetrofitServiceManager.getInstance().create(GameReq.class);
    }

    /**
     * 获取游戏列表
     * @param iCallback 返回类型 List<GameApiGameResp>
     */
    public void getGame(ICallback iCallback){
        //创建请求
        Call<MyResponse<List<GameApiGameResp>>> call=gameReq.getGame();
        //发出请求
        call.enqueue(new Callback<MyResponse<List<GameApiGameResp>>>() {
            @Override
            public void onResponse(Call<MyResponse<List<GameApiGameResp>>> call, Response<MyResponse<List<GameApiGameResp>>> response) {
                if(response.isSuccessful()){
                    iCallback.onSuccess(response.body().getData());
                }else {
                    iCallback.onFailed("错误码500");
                }
            }

            @Override
            public void onFailure(Call<MyResponse<List<GameApiGameResp>>> call, Throwable t) {
                iCallback.onFailed("网络错误");
            }
        });
    }

    /**
     * 通过ID获取游戏
     * @param id 游戏id
     * @param iCallback 返回类型 GameApiGetGameResp
     */
    public void getGameById(Integer id,ICallback iCallback){
        Call<MyResponse<GameApiGetGameResp>> call=gameReq.getGameById(id);
        call.enqueue(new Callback<MyResponse<GameApiGetGameResp>>() {
            @Override
            public void onResponse(Call<MyResponse<GameApiGetGameResp>> call, Response<MyResponse<GameApiGetGameResp>> response) {
                if(response.isSuccessful()){
                    iCallback.onSuccess(response.body().getData());
                }else {
                    if(response.body()!=null){
                        iCallback.onFailed(response.body().getErrmsg());
                    }else {
                        iCallback.onFailed("未知错误");
                    }
                }
            }

            @Override
            public void onFailure(Call<MyResponse<GameApiGetGameResp>> call, Throwable t) {
                iCallback.onFailed("网络错误");
            }
        });
    }

    /**
     * 获取游戏排行榜
     * @param pn 页码, 默认为1
     * @param limit 每页显示的个数, 取值范围为15到30, 默认为30
     * @param iCallback 返回类型 GameApiRankResp
     */
    public void getGameRank(Integer pn,Integer limit,ICallback iCallback){
        Call<MyResponse<GameApiRankResp>> call=gameReq.getGameRank(pn, limit);
        call.enqueue(new Callback<MyResponse<GameApiRankResp>>() {
            @Override
            public void onResponse(Call<MyResponse<GameApiRankResp>> call, Response<MyResponse<GameApiRankResp>> response) {
                if(response.isSuccessful()){
                    MyResponse<GameApiRankResp> myResponse=response.body();
                    if(myResponse!=null){
                        if(myResponse.getCode()==200){
                            iCallback.onSuccess(myResponse.getData());
                        }else {
                            iCallback.onFailed(myResponse.getErrmsg());
                        }
                    }else {
                        iCallback.onFailed("返回体为空");
                    }
                }else {
                    iCallback.onFailed("错误码500");
                }
            }

            @Override
            public void onFailure(Call<MyResponse<GameApiRankResp>> call, Throwable t) {
                iCallback.onFailed("网络错误");
            }
        });
    }

    /**
     * 获取游戏推荐列表
     * @param iCallback 返回类型List|GameResp
     */
    public void getGameRecommend(ICallback iCallback){
        Call<MyResponse<List<GameResp>>> call=gameReq.getGameRecommend();
        call.enqueue(new Callback<MyResponse<List<GameResp>>>() {
            @Override
            public void onResponse(Call<MyResponse<List<GameResp>>> call, Response<MyResponse<List<GameResp>>> response) {
                if(response.isSuccessful()){
                    MyResponse<List<GameResp>> myResponse= response.body();
                    if(myResponse!=null){
                        if(myResponse.getCode()==200){
                            iCallback.onSuccess(myResponse.getData());
                        }else {
                            iCallback.onFailed(myResponse.getErrmsg());
                        }
                    }else {
                        iCallback.onFailed("响应为空");
                    }
                }else {
                    iCallback.onFailed("错误码500");
                }
            }

            @Override
            public void onFailure(Call<MyResponse<List<GameResp>>> call, Throwable t) {
                iCallback.onFailed("网络错误");
            }
        });
    }

    /**
     *
     * @param id 游戏id
     * @param token token
     * @param iCallback 返回类型 Boolean
     */
    public void postGameIdFavor(Integer id,String token,ICallback iCallback){
        Call<MyResponse<Boolean>> call=gameReq.postGameIdFavor(id,token);
        call.enqueue(new Callback<MyResponse<Boolean>>() {
            @Override
            public void onResponse(Call<MyResponse<Boolean>> call, Response<MyResponse<Boolean>> response) {
                if(response.isSuccessful()){
                    MyResponse<Boolean> myResponse=response.body();
                    if(myResponse!=null){
                        if(myResponse.getCode()==200){
                            iCallback.onSuccess(myResponse.getData());
                        }else {
                            iCallback.onFailed(myResponse.getErrmsg());
                        }
                    }else {
                        iCallback.onFailed("响应为空");
                    }
                }else {
                    iCallback.onFailed("错误码500");
                }
            }

            @Override
            public void onFailure(Call<MyResponse<Boolean>> call, Throwable t) {
                iCallback.onFailed("网络错误");
            }
        });
    }

    /**
     * 用户取消收藏一个游戏
     * @param id 游戏id
     * @param token token
     * @param iCallback 返回类型 Boolean
     */
    public void deleteGameIdFavor(Integer id,String token,ICallback iCallback){
        Call<MyResponse<Boolean>> call=gameReq.deleteGameIdFavor(id,token);
        call.enqueue(new Callback<MyResponse<Boolean>>() {
            @Override
            public void onResponse(Call<MyResponse<Boolean>> call, Response<MyResponse<Boolean>> response) {
                if(response.isSuccessful()){
                    MyResponse<Boolean> myResponse=response.body();
                    if(myResponse!=null){
                        if(myResponse.getCode()==200){
                            iCallback.onSuccess(myResponse.getData());
                        }else {
                            iCallback.onFailed(myResponse.getErrmsg());
                        }
                    }else {
                        iCallback.onFailed("响应为空");
                    }
                }else {
                    iCallback.onFailed("错误码500");
                }
            }

            @Override
            public void onFailure(Call<MyResponse<Boolean>> call, Throwable t) {
                iCallback.onFailed("网络错误");
            }
        });
    }

    /**
     * 搜索游戏
     * @param key 搜索关键字, 会从游戏名、分类名、简介中进行搜索
     * @param pn 页码, 默认为1
     * @param limit 每页显示的个数, 取值范围为5到10, 默认为10
     * @param iCallback 返回类型GameApiSearchResp
     */
    public void getGameSearch(String key,Integer pn,Integer limit,ICallback iCallback){
        Call<MyResponse<GameApiSearchResp>> call=gameReq.getGameSearch(key, pn, limit);
        call.enqueue(new Callback<MyResponse<GameApiSearchResp>>() {
            @Override
            public void onResponse(Call<MyResponse<GameApiSearchResp>> call, Response<MyResponse<GameApiSearchResp>> response) {
                if(response.isSuccessful()){
                    MyResponse<GameApiSearchResp> myResponse= response.body();
                    if(myResponse!=null){
                        if(myResponse.getCode()==200){
                            iCallback.onSuccess(myResponse.getData());
                        }else {
                            iCallback.onFailed(myResponse.getErrmsg());
                        }
                    }else {
                        iCallback.onFailed("响应体为空");
                    }
                }else {
                    iCallback.onFailed("错误码500");
                }
            }

            @Override
            public void onFailure(Call<MyResponse<GameApiSearchResp>> call, Throwable t) {
                iCallback.onFailed("网络错误");
            }
        });
    }
}
